Skip to content

feat: per-entry hooks to filter, reshape, and rewrite snapshots - #39

Merged
antfu merged 1 commit into
mainfrom
feat/per-entry-snapshot-hooks
Sep 14, 2026
Merged

antfu merged 1 commit into
mainfrom
feat/per-entry-snapshot-hooks

Conversation

@antfubot

Copy link
Copy Markdown
Collaborator

Closes #38

Adds three per-entry hooks to ApiSnapshotOptions, at increasing depth of intervention:

  • entryFilter(ctx) — skip an entry point entirely (ctx: { packageName, entryName }; return false to skip). Solves the UnoCSS case of dropping low-signal data entries like ./theme / ./colors while keeping the rest of the package guarded.
  • transformEntries(entries, ctx) — modify the internal structural representation (the extracted Entry[] of { name, kind, text } per surface) before it is serialized: mutate in place or return a replacement array. Enables e.g. collapsing a known large object export to an opaque declaration while everything else stays guarded.
  • transformSnapshot(ctx) — rewrite the final snapshot string (header excluded) before it is written or compared; return the new string or null/undefined to keep it.

Because the hooks live on ApiSnapshotOptions and are bound through a single shared createEntryHooks helper, they behave identically across every integration: snapshotApiPerEntry / describePackagesApiSnapshots (Vitest), generateApiSnapshot, snapshotPackage / snapshotFiles, and the rolldown plugin. In the Vitest integration, filtered entries are skipped at test-registration time (no dangling it blocks), and transformed content flows through the breaking-change update guard, so the guard compares what will actually be written.

Also, as noted in the issue, typeWidening and categorizedExports were not forwarded through the Vitest SnapshotApiOptions — they now are, and the Entry / EntryKind / hook-context types are exported from both tsnapi and tsnapi/vitest.

Covered by new core tests (test/hooks.test.ts) and a hooked fixture in the Vitest integration test with committed snapshots.


This PR was created with the help of an agent.

@antfu
antfu merged commit 4855773 into main Sep 14, 2026
4 checks passed
@antfu
antfu deleted the feat/per-entry-snapshot-hooks branch September 14, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: hook to filter/transform snapshot content per entry before writing

2 participants